/**
 * Created by admin on 2017/6/9.
 */
import {
  CoachSignDetail,
  SignList,
  MemberDetail
} from '../mutation-types';
import { Cache, cache_keys } from '../../utils/util';
//查询教练带队活动列表
export const queryMyLeadActivity =function({commit,state},data){
   let params ={},
   index=data.index || 0,
   page =data.page ||1;
   if(index!=0){
      params.status=index+1;
   }
   params.page=page;
   let oauth = Cache.get(cache_keys.token);
   EventBus.$http.get(EventBus.$Config.baseUrl+"api/activity/coachActList",{
     headers: {
       Authorization: oauth.access_token
     },
     params
   }).then(res=>{
      if(res.data.status){
         EventBus.$emit('coach-activityList',res.data,index);
      }
      else{
        EventBus.$Modal.info({title:"消息提示",body:res.data.message || '加载错误'});
      }
   },()=>{
     EventBus.$Modal.info({title:"消息提示",body: '加载失败'});
   });
};

// 查看教练版报名详情
export const queryCoachSignDetail =function({commit,state},id){
  let oauth = Cache.get(cache_keys.token);
  EventBus.$http.get(EventBus.$Config.baseUrl+"api/activity/coachAct/"+id,{
    headers: {
      Authorization: oauth.access_token
    }
  }).then( res=>{
     if(res.data.status){
        let data=res.data.data;
        commit(CoachSignDetail,res.data);
     }
     else{
       EventBus.$Modal.info({title:"消息提示",body:res.data.message || '加载错误'});
     }
  },()=>{
    EventBus.$Modal.info({title:"消息提示",body: '加载失败'});
  });
};


//查询活动报名人列表
export const querySignList =function({commit,state},id){
    let oauth =Cache.get(cache_keys.token);
    EventBus.$http.get(EventBus.$Config.baseUrl+'api/activity/memberList/'+id,{
      headers: {
        Authorization: oauth.access_token
      }
    }).then(res =>{
       if(res.data.status){
         let data=res.data.data;
         data.signed.forEach(v=>v.showTel =false);
         commit(SignList,data);
       }else{
         EventBus.$Modal.info({title:"消息提示",body: res.data.message || "加载错误"});
       }
    },()=>{
      EventBus.$Modal.info({title:"消息提示",body:  "加载失败"});
    })
};


// 查看活动成员详情
export const queryMemberDetail =function({commit,state},data){
    let oauth = Cache.get(cache_keys.token),
        page =data.page || 1;
    EventBus.$http.get(EventBus.$Config.baseUrl+'api/activity/memberInfo/'+data.id,{
      headers: {
        Authorization: oauth.access_token
      },
      params: {
        page
      }
    }).then(res => {
       if(res.data.status){
          if(page>1){
             res.data.data =state.memberDetail.data.concat(res.data.data);
          }
          commit(MemberDetail,res.data);
           if (res.data.meta && res.data.meta.pagination){
             EventBus.$emit('page-loaded', true, res.data.meta.pagination);
           } else {
             EventBus.$emit('page-loaded', false);
           }
           EventBus.$emit('member-complete',res.data.meta.user);
       }
       else{
         EventBus.$Modal.info({title:"消息提示",body:res.data.message || '加载错误'});
       }
    },()=>{
      EventBus.$Modal.info({title:"消息提示",body:'加载失败'});
    });
};

// 奖励积分
export const submitreward =function({commit,state},data){
  let oauth = Cache.get(cache_keys.token);
  EventBus.$http.post(EventBus.$Config.baseUrl+'api/activity/rewards/'+data.id,{
      user_ids:data.user_ids,
      value:data.reward_value
  },{
     headers:{
       Authorization:oauth.access_token
     }
  }).then(res=>{
    if(res.data.status){
      EventBus.$emit('reward-complete');
    }
    else{
      EventBus.$Modal.info({title:"消息提示",body:res.data.message || '操作失败'});
    }
  },()=>{
    EventBus.$Modal.info({title:"消息提示",body:'加载失败'});
  });
}
